<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="商户ID">
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item label="币种">
          <el-input v-model="form.currency" placeholder="币种" />
        </el-form-item>
        <el-form-item label="交易时间">
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
            :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column
            prop="start_time"
            label="开始时间"
            :formatter="formatterTime"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="end_time"
            label="截止时间"
            :formatter="formatterTime"
            align="center"
          ></el-table-column>
          <el-table-column prop="merchant_id" label="商户id" align="center" />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
            width="200"
          />
          <el-table-column prop="currency" label="币种" align="center" />
          <el-table-column
            prop="opening_balance"
            label="期初余额"
            align="center"
          />
          <el-table-column
            prop="opening_freeze"
            label="期初冻结"
            align="center"
          />
          <el-table-column
            prop="added_amount"
            label="本期增加"
            align="center"
          />
          <el-table-column
            prop="decreased_amount"
            label="本期减少"
            align="center"
          />
          <el-table-column
            prop="closing_balance"
            label="期末余额"
            align="center"
          />
          <el-table-column
            prop="closing_freeze"
            label="期末冻结"
            align="center"
          />
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import {
  formatterTime,
  pickerDefaultTime,
  timestamp,
  exportOrDownload,
} from "@/utils/utils";
import { payMerAssetStatsApi } from "@/api/ExAssistant/payassets";

const loading = ref(false);
const tableData = ref([]);
const tradeType = ref([]);
const btnType = ref(null);
const formData = {
  merchant_id: "",
  currency: "",
  timeData: [],
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    merchant_id: form.merchant_id,
    currency: form.currency,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await payMerAssetStatsApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 查询
const onQuery = () => {
  //    console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  const res = await payMerAssetStatsApi(data, "blob");
  exportOrDownload(res, "商户财务统计");
};

onMounted(() => {
  getTbaleData();
});
</script>
